package com.wanxi.test;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManageSystem {
    public static void main(String[] args) {

//        System.out.println("请输入你的选择");
//        Scanner scanner = new Scanner(System.in);
//        Tool tool = new Tool();
        ArrayList<Student> list = new ArrayList<>();
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看所有学生");
            System.out.println("5 退出");
            String aa = Tool.scanner("请输入你的选择");

            switch (aa) {
                case "1":
                    add(list);
                    break;
                case "2":
                    delete(list);

                    break;
                case "3":
                    update(list);
                    break;
                case "4":
                    findAll(list);
                    break;
                case "5":
//                Java虚拟机正常退出
                    System.exit(0);
                    break;
            }
        }
    }

    private static void findAll(ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

    private static void update(ArrayList<Student> list) {
        String name = Tool.scanner("请输入您要修改的学生姓名");
        boolean isHave = false;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals(name)) {
                list.set(i, getStudent());
                isHave = true;
            }
        }
        if (isHave) {
            System.out.println("修改后的学生信息为");
            findAll(list);
        } else {
            System.out.println("你输入的姓名不存在");
        }
    }

    private static void delete(ArrayList<Student> list) {
        String name = Tool.scanner("请输入您要删除的学生姓名");
        boolean isHave = false;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals(name)) {
                list.remove(list.get(i));
                isHave = true;
            }
        }
        if (isHave) {
            System.out.println("删除成功，剩下的学生为");
            findAll(list);
        } else {
            System.out.println("你输入的姓名不存在");
        }

    }

    private static void add(ArrayList<Student> list) {
//        Tool tool = new Tool();
        String str = Tool.scanner("请输入学生信息：格式为：姓名,年龄");
        String[] split = str.split(",");
        Student student = new Student(split[0], Integer.valueOf(split[1]));
        boolean isHave = false;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals(student.getName())) {
                isHave = true;
                break;
            }
        }
        if (isHave) {
            System.out.println("你输入的学生已经存在,请重新输入");
        } else {
            list.add(student);
        }

    }

    private static Student getStudent() {
        String str = Tool.scanner("请输入学生信息：格式为：姓名,年龄");
        String[] split = str.split(",");
        Student student = new Student(split[0], Integer.valueOf(split[1]));
        return student;
    }
}
